﻿Imports System.Runtime.InteropServices

Public Class frmMain

    Private Shared simulation As Integer
    Private Shared simulationState As String

    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Function toInt(ByVal v As String) As Integer
    End Function

    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Function createSimulation() As Integer
    End Function

    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Sub destroySimulation(ByVal instancePtr As Integer)
    End Sub


    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Function createSetupParams(ByVal lanes_N As Integer, ByVal lanes_S As Integer, ByVal lanes_E As Integer, ByVal lanes_W As Integer, ByVal probability_N As Integer, ByVal probability_S As Integer, ByVal probability_E As Integer, ByVal probability_W As Integer) As Integer
    End Function


    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Sub startSimulation(ByVal instancePtr As Integer, ByVal setupParameters As Integer)
    End Sub

    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Function runCycle(ByVal instancePtr As Integer) As Boolean
    End Function

    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Sub stopSimulation(ByVal instancePtr As Integer)
    End Sub


    <DllImport("TrafficSimulator.dll", CallingConvention:=CallingConvention.StdCall)> Private Shared Sub setVisWindowTitle(ByVal instancePtr As Integer, ByVal title As String)
    End Sub

    Private Sub frmMain_Unload(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.HandleDestroyed
        FreeMemory()
    End Sub

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        simulation = createSimulation()
        stopRunningSimulation()

        'Populate the developer controls
        updownCycleTimerInterval.Value = cycleTimer.Interval
    End Sub

    Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
        simulationState = "running"
        btnStart.Enabled = False
        btnStop.Enabled = True
        btnPauseResume.Enabled = True
        cycleTimer.Enabled = True
        Dim setup As Integer = createSetupParams(updownLanesN.Value, updownLanesS.Value, updownLanesE.Value, updownLanesW.Value, updownProbabilityN.Value, updownProbabilityS.Value, updownProbabilityE.Value, updownProbabilityW.Value)
        startSimulation(simulation, setup)
        setVisWindowTitle(simulation, "Running")
    End Sub

    Private Sub FreeMemory()
        If simulationState <> "stopped" Then stopRunningSimulation()
        If simulation <> 0 Then
            destroySimulation(simulation)
            simulation = 0
        End If
    End Sub

    Private Sub btnStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStop.Click
        stopRunningSimulation()
    End Sub

    Private Sub stopRunningSimulation()
        simulationState = "stopped"
        btnStop.Enabled = False
        btnPauseResume.Text = "Pause"
        btnPauseResume.Enabled = False
        cycleTimer.Enabled = False
        stopSimulation(simulation)
        setVisWindowTitle(simulation, "Stopped")
        btnStart.Enabled = True
    End Sub

    Private Sub cycleTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cycleTimer.Tick
        If runCycle(simulation) = False Then
            cycleTimer.Enabled = False
            stopRunningSimulation()
            setVisWindowTitle(simulation, "Stopped due to Failure")
            MsgBox("The intersection has been backed up by congestion!", MsgBoxStyle.Critical, "Intersection Failed")
        End If
    End Sub

    Private Sub btnPauseResume_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPauseResume.Click
        If btnPauseResume.Text = "Pause" Then
            cycleTimer.Enabled = False
            btnPauseResume.Text = "Resume"
            setVisWindowTitle(simulation, "Paused")
        Else
            cycleTimer.Enabled = True
            btnPauseResume.Text = "Pause"
            setVisWindowTitle(simulation, "Running")
        End If
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        FreeMemory()
        End
    End Sub

    Private Sub AboutToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutToolStripMenuItem.Click
        frmAbout.Show()
    End Sub

    Private Sub btnApplyRuntimeSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApplyRuntimeSettings.Click
        'Apply runtime developer settings
        cycleTimer.Interval = updownCycleTimerInterval.Value
    End Sub
End Class
